package base.thread_demo.demo14;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockDemo0 {
    private static final Logger log = LoggerFactory.getLogger(ReadWriteLockDemo0.class);

    static ReadWriteLock lock = new ReentrantReadWriteLock();

    public static void main(String[] args) {
//        error1();
        success1();
    }

    public static void error1(){
        lock.readLock().lock();
        lock.writeLock().lock();
        log.info("...........error1.........");
        lock.writeLock().unlock();
        lock.readLock().unlock();
    }

    // SUCCESS
    public static void success1(){
        lock.writeLock().lock();// t1上了写锁后,后面读锁一定是t1上的,锁的降级(锁的重入)
        lock.readLock().lock();
        log.info(".........success1.......");
        lock.writeLock().unlock();
        lock.readLock().unlock();
    }

}
